package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.IMemberService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/shopping")
@Api(tags = "OrderController", description = "订单控制层")
public class OrderController {
    @Reference(check = false)
    IMemberService iMemberService;

    @Reference(timeout = 3000, check = false)
    private OrderCoreService orderCoreService;

    @Reference(timeout = 3000, check = false)
    private OrderQueryService orderQueryService;

    /**
     * 创建订单
     */
    @PostMapping("/order")
    @ApiOperation("创建订单")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest servletRequest) {
        // 获取 userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        System.out.println("token =  " + userInfo);
        Long uid = Long.valueOf(iMemberService.cartSelectById(userInfo));
        System.out.println(" userId =   " + uid);

        createOrderRequest.setUserId(uid);

        // 生成业务唯一 id
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());

        // 执行创建订单的业务逻辑
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);

        if (createOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(createOrderResponse.getOrderId());
        }

        return new ResponseUtil<>().setErrorMsg(createOrderResponse.getMsg());
    }

    /**
     * 获取订单
     */
    @GetMapping("/order")
    @ApiOperation("获取订单")
    public ResponseData getOrder(Integer size, Integer page, HttpServletRequest servletRequest) {
        // 获取 userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);

        Long uid = Long.valueOf(iMemberService.cartSelectById(userInfo));

        OrderListResponse orderListResponse = orderQueryService.getOrderList(uid, size, page);
        System.out.println("orderListResponse =   " + orderListResponse);

        return new ResponseUtil<>().setData(orderListResponse);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/order/{orderId}")
    @ApiOperation("删除订单")
    public ResponseData deleteOrder(@PathVariable("orderId") String orderId, HttpServletRequest servletRequest) {
        // 获取 userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        Integer uid = iMemberService.cartSelectById(userInfo);
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(orderId);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        return new ResponseUtil().setData(deleteOrderResponse.getMsg());
    }

    /**
     * 订单详情
     */
    @GetMapping("/order/{orderId}")
    @ApiOperation("订单详情")
    public ResponseData getOrderDetail(@PathVariable("orderId") String orderId, HttpServletRequest servletRequest) {
        String  userInfo = (String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);

        OrderDetailResponseNew orderDetailResponse = orderQueryService.getOrderDetail(userInfo, orderId);

        return new ResponseUtil<>().setData(orderDetailResponse);
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancelOrder")
    @ApiOperation("取消订单")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
        System.out.println("取消订单----------------------");
        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
        return new ResponseUtil<>().setData(cancelOrderResponse);
    }
}
